package com.frost.musicplayer.controller;

import com.frost.musicplayer.mapper.LoveMusicMapper;
import com.frost.musicplayer.model.Music;
import com.frost.musicplayer.model.User;
import com.frost.musicplayer.tools.Constant;
import com.frost.musicplayer.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Autowired
    private LoveMusicMapper loveMusicMapper;

    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        System.out.println("musicId: " + musicId);

        // 1. 检查是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录!");
            return new ResponseBodyMessage<>(-1, "请登录后收藏", false);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId: " + userId);

        Music music = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId , musicId);
        if (music != null) {
            // 之前收藏过， 不能进行收藏了 TODO:加一个取消收藏
            return new ResponseBodyMessage<>(-1, "已收藏过此音乐", false);
        }

        boolean effect = loveMusicMapper.insertLoveMusic(userId, musicId);;
        if (effect) {
            return new ResponseBodyMessage<>(0, "收藏成功", true);
        }else {
            return new ResponseBodyMessage<>(-1, "收藏失败", false);
        }

//        try {
//            int ret = 0;
//            ret = loveMusicMapper.insertLoveMusic(userId, musicId);
//            if (ret == 1) {
//                return new ResponseBodyMessage<>(0, "收藏成功", true);
//            }else {
//                return new ResponseBodyMessage<>(-1, "收藏失败", false);
//            }
//        }catch (BindingException e) {
//            return new ResponseBodyMessage<>(-1, "111收藏失败", false);
//        }
    }

    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName, HttpServletRequest request) {
        // 1. 检查是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录!");
            return new ResponseBodyMessage<>(-1, "请登录后查找", null);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId: " + userId);

        List<Music> musicList = null;
        if (musicName == null) {
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        }else {
            musicList = loveMusicMapper.findLoveMusicByKeyAndUID(musicName, userId);
        }
        return new ResponseBodyMessage<>(0, "查询到所有喜欢的音乐", musicList);
    }

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id, HttpServletRequest request) {
        // 1. 检查是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录!");
            return new ResponseBodyMessage<>(-1, "请登录后移除", false);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId: " + userId);

        int musicId = Integer.parseInt(id);

        int ret = loveMusicMapper.deleteLoveMusic(userId, musicId);

        if (ret == 1) {
            return new ResponseBodyMessage<>(0, "删除喜欢的音乐成功", true);
        }else {
            return new ResponseBodyMessage<>(-1, "取消收藏失败", false);
        }
    }
}
